package com.vitabuybuy.tv.ui.mine.wallet

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityWithdrawBinding
import com.vitabuybuy.tv.dto.BankCardListDto
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.UniversalityResultActivity
import com.vitabuybuy.tv.ui.mine.wallet.viewmodel.WithdrawViewMolde
import com.vitabuybuy.tv.utils.StringUtil
import kotlinx.android.synthetic.main.activity_withdraw.*

class WithdrawActivity : BaseActivity<WithdrawViewMolde, ActivityWithdrawBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_withdraw

    private var amount: Double? = 0.00
    private var bankCardId = -1
    override fun onGetBundle(bundle: Bundle?) {
        amount = bundle?.getDouble("amount")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.withdraw))
    }

    override fun observeData() {
        viewModel.withdrawRate()
        viewModel.withDrawRate.observe(this, Observer {
            tvAvailableBalance.text = String.format(getString(R.string.withdraw_info), amount, it)
        })
    }

    override fun eventHandling() {
        tvAllWithDraw.setOnClickListener {
            etAmount.setText(amount.toString())
        }
        rlChooseAccount.setOnClickListener {
            ChooseAccountActivity.openActivity(mContext, 0x11)
        }
        btnWithDraw.setOnClickListener {
            if (bankCardId == -1) {
                XToast.normal(getString(R.string.pls_choose_bank_card))
                return@setOnClickListener
            }
            var withAmount = etAmount.text.toString()
            if (withAmount.isEmpty()) {
                XToast.normal(getString(R.string.pls_enter_with_amount))
                return@setOnClickListener
            }
            if (withAmount.startsWith(".")) {
                withAmount = "0$withAmount"
            }
            if (withAmount.endsWith(".")) {
                withAmount += "00"
            }
            if (withAmount.toBigDecimal().compareTo(amount?.toBigDecimal()) == 1) {
                XToast.normal(getString(R.string.more_than_amount))
                return@setOnClickListener
            }
            viewModel.withDraw(bankCardId, withAmount)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        if (requestCode == 0x11) {
            val bankCardDto = data?.extras?.getSerializable("bankDto") as BankCardListDto
            bankCardId = bankCardDto.id
            rlChooseAccount.text = bankCardDto.bankName + "(" + String.format(
                getString(R.string.weihao),
                bankCardDto.number
            ) + ")"
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                UniversalityResultActivity.openActivity(
                    mContext,
                    UniversalityResultActivity.FROM_WITH_DRAW_SUCCESS
                )
                finish()
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, amount: Double?) {
            mContext.startActivity(bundleOf(Pair("amount", amount)), WithdrawActivity::class.java)
        }
    }
}
